package com.demo.interceptor;


import com.demo.util.CommonsUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;


/**
 * 获取信息的拦截器
 */
@Component
public class TestInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        try {
            //获取uri
            String uri = request.getRequestURI();
            //获取session对象
            HttpSession session = request.getSession();
            //获取handler操作
            if (handler instanceof HandlerMethod) {
                HandlerMethod handlerMethod = (HandlerMethod) handler;
                Method method = handlerMethod.getMethod();
                //如果在controller的方法上加上自定义注解 可以在这里用method.getAnnotation(注解名字.class);
                // 判断是否包含此注解 也可以获取其注解的值进行特定的操作
                //if(method.getAnnotations()!=null){ } 获取所有注解
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
